Page({
  data: {
    functionList: [{
      name: "我收藏的音乐",
      iconClass: "cuIcon-choicenessfill",
      pagePath: "/pages/collection/music/index?type=music"
    }, {
      name: "我收藏的榜单",
      iconClass: "cuIcon-weixin",
      pagePath: "/pages/collection/list/index?type=bang"
    }, {
      name: "我收藏的歌单",
      iconClass: "cuIcon-markfill",
      pagePath: "/pages/collection/list/index?type=song"
    }, {
      name: "我收藏的歌手",
      iconClass: "cuIcon-forwardfill",
      pagePath: "/pages/collection/list/index?type=singer"
    }]
  },
  onLoad() {
    let userinfo = wx.getStorageSync('userinfo')
    if (userinfo) {
      this.setData({
        userinfo
      })
    } else {
      this.setData({
        userinfo: {
          avatarUrl: "https://img1.baidu.com/it/u=3598104138,3632108415&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800",
          nickName: "微信用户",
        }
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
    }
  },
  onShow() {
    const music = wx.getStorageSync('collectionMusic') || []
    const song = wx.getStorageSync('collectionSong') || []
    const bang = wx.getStorageSync('collectionBang') || []
    const singer = wx.getStorageSync('collectionSinger') || []
    this.setData({
      'functionList[0].count': music.length || '',
      'functionList[1].count': bang.length || '',
      'functionList[2].count': song.length || '',
      'functionList[3].count': singer.length || '',
    })
  },
  // 更换用户头像
  async chooseAvatarImage(e) {
    if (e.detail.avatarUrl) {
      let filePath = e.detail.avatarUrl
      this.setData({
        'userinfo.avatarUrl': filePath
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
    }
  },
  // 昵称输入
  nicknameInput(e) {
    this.nickname = e.detail.value
  },
  // 更换昵称
  async nicknamereview(e) {
    console.log(e)
    if (e.detail.pass && this.nickname) {
      this.setData({
        'userinfo.nickName': this.nickname
      })
      wx.setStorageSync('userinfo', this.data.userinfo)
    } else {
      this.setData({
        'userinfo.nickName': this.data.userinfo.nickName
      })
    }
  },
  toNextPage(e) {
    let {
      index
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: this.data.functionList[index].pagePath,
    })
  },
})